<template>
  <van-icon
    :color="value ? '#ffa500' : '#777'"
    :name="value ? 'star' : 'star-o'"
    @click="isCollect"
    :loading="loading"
  />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'collectArticle',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Object, String, Number],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async isCollect () {
      this.loading = true
      try {
        if (this.value) {
          await deleteCollect(this.artId)
        } else {
          await addCollect(this.artId)
        }
        this.$emit('input', !this.value)
        this.$toast.success(this.value ? '取消收藏' : '收藏成功')
      } catch (err) {
        console.log(err)
        this.$toast('操作失败，请重试')
      }
      this.loading = false
    }
  }
}
</script>

<style>
</style>
